package niuke;

/**
 * 二叉树的子结构
 */
public class Solution1 {
    public boolean isSon( TreeNode root1, TreeNode root2) {
        if ( root1 == null && root2 == null) return true;
        if ( root1 == null && root2 != null) return false;
        if ( root1 != null && root2 == null) return true;

        if (root1.val != root2.val) return false;
        return isSon(root1.left, root2.left) &&
               isSon(root1.right, root2.right);
    }
    public boolean HasSubtree(TreeNode root1, TreeNode root2) {
        if ( root2 == null) return false;
        if ( root1 == null && root2 != null) return false;
        if ( root1 == null || root2 == null) return true;

        boolean flag1 = isSon(root1, root2);
        boolean flag2 = HasSubtree(root1.left, root2);
        boolean flag3 = HasSubtree(root1.right, root2);
        return flag1 || flag2 || flag3;
    }
}